package cn.wolfcode.trip.app.utils;
import cn.wolfcode.trip.base.domain.PersonalMessage;
import cn.wolfcode.trip.base.domain.User;
import cn.wolfcode.trip.base.query.PersonalMessageQueryObject;
import cn.wolfcode.trip.base.service.IPersonalMessageService;
import cn.wolfcode.trip.base.service.IUserService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.socket.CloseStatus;
import org.springframework.web.socket.TextMessage;
import org.springframework.web.socket.WebSocketSession;
import org.springframework.web.socket.handler.TextWebSocketHandler;

import java.util.ArrayList;
import java.util.Map;

public class MyHandler extends TextWebSocketHandler {
    @Autowired
    private IPersonalMessageService personalMessageService;

    @Override
    public void afterConnectionEstablished(WebSocketSession session) throws Exception {
        /*PersonalMessageQueryObject qo = new PersonalMessageQueryObject();

        Map<String, Object> attributes = session.getAttributes();
        User o = (User) attributes.get("USER_IN_SESSION");
        qo.setGetterId(o.getId());
        qo.setSenderId(43L);
        qo.setState(1);
        PageInfo<PersonalMessage> result = personalMessageService.queryForChatRecords(qo);
        ArrayList<String> strings = new ArrayList<String>();
        for (PersonalMessage p:result.getList()){
            byte[] bytes = p.getJson().getBytes();
            session.sendMessage(new TextMessage(bytes));
        }*/


    }

    @Override
    protected void handleTextMessage(WebSocketSession session, TextMessage message) throws Exception {
        super.handleTextMessage(session, message);
        /*System.out.println(message);
        User user_in_session = (User) session.getAttributes().get("USER_IN_SESSION");
        System.out.println(user_in_session.getNickName());
        TextMessage returnMessage = new TextMessage(" received at server");
        System.out.println(returnMessage);
        session.sendMessage(returnMessage);*/
    }

    @Override
    public void afterConnectionClosed(WebSocketSession session, CloseStatus status) throws Exception {
        System.out.println("close");
        super.afterConnectionClosed(session, status);
    }
}
